package com.luoying.itew.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.luoying.itew.R;
import com.luoying.itew.bean.CollectorBean;
import com.luoying.itew.common.Constant;
import com.luoying.itew.util.MonitorUtils;
import com.luoying.itew.util.ZXWUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by lufan on 2018/5/29
 * Class describe 采集器详情对话框
 */
public class CollectorDialog extends Dialog implements View.OnClickListener {
    private TextView infoTv;
    private CollectorBean bean;
    public boolean owner_gnss;

    public CollectorDialog(@NonNull Context context, CollectorBean bean) {
        super(context, R.style.collector_dig_style);
        this.bean = bean;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_collect_layout, null);
        setContentView(view);
        infoTv = (TextView) view.findViewById(  R.id.collector_detain_info);
        StringBuffer buffer = new StringBuffer();
        buffer.append("名称：").append(bean.getCollectorName()).append("\n")
                .append("监测点ID: ").append(bean.getCollectorId()).append("\n");
        int state = MonitorUtils.CollectorLevel(bean);
        if(state!=Constant.WARN_LEVEL_GREEN){
            String stateString;
            switch (state){
                default:
                case Constant.WARN_LEVEL_YELLOW:
                    stateString = context.getString(R.string.warn_leve_yellow);
                    break;
                case Constant.WARN_LEVEL_ORANGE:
                    stateString = context.getString(R.string.warn_leve_orange);
                    break;
                case Constant.WARN_LEVEL_RED:
                    stateString = context.getString(R.string.warn_leve_red);
                    break;
            }
            buffer.append("状态：").append(stateString).append("\n");
            buffer.append("类型：").append(bean.getWarnTypeDesc()).append("\n");
            try {
                buffer.append("时间：").append(simpleDateFormat.format(new Date(Long.parseLong(bean.getWarnTime())))).append("\n");
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        } else {
            buffer.append("状态：").append(context.getString(R.string.mountain_state_fine)).append("\n");
        }
        //区分倾角位移
        // 0 gnss 1位移 2水位 3 温湿度 4 大气压 5倾角 8 沉降 9 自沉降 10测斜仪
        int type = bean.getCollectorType();
        switch (type){
            case 0:
                buffer.append("累计水平位移(毫米)：").append(ZXWUtils.ThreeToString(bean.getAccumulativeDisplacementX())).append("\n");
                buffer.append("累计沉降位移(毫米)：").append(ZXWUtils.ThreeToString(bean.getAccumulativeDisplacementY()));
                break;
            case 8:
                buffer.append("相对沉降(毫米): ").append(bean.getAccumulativeDisplacementX());
                break;
            case 1:
                buffer.append("累计位移(毫米): ").append(bean.getAccumulativeDisplacement());
                break;
            case 2:
                buffer.append("液位高度(米):").append(bean.getAccumulativeDisplacement());
                break;
            case 5:
                if(owner_gnss){
                    //有gnss 显示角度
                    buffer.append("X轴累计倾斜角(°): ").append(ZXWUtils.ThreeToString(bean.getLastX())).append("\n");
                    buffer.append("Y轴累计倾斜角(°): ").append(ZXWUtils.ThreeToString(bean.getAccumulativeDisplacementX()));
                } else {
                    buffer.append("X轴累计水平位移(毫米): ").append(ZXWUtils.ThreeToString(bean.getAccumulativeDisplacementX())).append("\n");
                    buffer.append("Y轴累计水平位移(毫米): ").append(ZXWUtils.ThreeToString(bean.getAccumulativeDisplacementY()));
                }
                break;
            default:
                break;
        }
        infoTv.setText(buffer.toString());
        findViewById(R.id.collector_dialog_close).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.collector_dialog_close){
            if(isShowing()){
                dismiss();
            }
        }
    }
}
